<?php
class Ab_Directory extends Ab_Path{
	private $path;
	private $relativePath;
	
	function __construct($path){
		parent::__construct($path);
		
		$this->path=rtrim($path,'/');
		$this->relativePath=substr($this->path,strlen(ROOT_DIR));
		
		if(!is_dir($this->path)){
			throw new NotDirectoryException('Path '.$this->path.' is not a directory');
		}
	}
	
	//get a single level
	public function getChildren($type=null){
		$children=array();
		
		if ($handle = opendir($this->path)) {
		    while (false !== ($file = readdir($handle))) {
		    	//skip .,.., .hidden files/folders		    	
		    	if($file=='.' || $file=='..' || (strpos($file, '.')!==false && strpos($file, '.')==0)) continue;
		    	
		    	switch($type){
		    		case 'file':
			    		if(is_file($this->path.'/'.$file)){
			    			$children[]=$file;	
			    		}
		    		break;
		    		case 'dir':
			    		if(is_dir($this->path.'/'.$file)){
			    			$children[]=$file;	
			    		}
		    		break;
		    		default:		    			
		    			$children[]=$file;
		    	}
			
			
		    }		  
		    closedir($handle);
		}
		sort($children);
		return $children;
	}
	
	public function getDirChildren(){
		$dirs_info=array();
		
		$files=$this->getChildren('dir');
		for($i=0;$i<count($files);$i++){			
			$dirs_info[]=array(
				'dir_name'=>$files[$i],
				'dir_path'=>$this->path.'/'.$files[$i]
			);
		}
		return $dirs_info;
	}
	
	public function getFileChildren(){
		$file_obj=null;
		$files_info=array();
		
		$files=$this->getChildren('file');
		for($i=0;$i<count($files);$i++){			
			$path=new Ab_Path($this->path.'/'.$files[$i]);						
			$file_obj=$path->getPathType();
			
			if($file_obj){
				$file_type=$file_obj->getType();
				$file_thumb=null;
						
				if($file_type=='image'){
					$file_thumb=$file_obj->getThumb();
				}
				$files_info[]=array(
				'file_name'=>$files[$i],
				'file_path'=>$this->path.'/'.$files[$i],
				'file_type'=>$file_type,
				'file_thumb'=>$file_thumb,
				'file_extension'=>$file_obj->getExtension()
				);
			}
		}
		return $files_info;
	}
	
	public function getUpperDirs(){	
		$dirs=explode('/',trim($this->relativePath,'/'));
		$paths=array();
		
		for($i=0;$i<count($dirs);$i++){			
			if(count($paths)>0){
				$last_path_index=count($paths)-1;
				$paths[]=array(
					'dir_path'=>$paths[$last_path_index]['dir_path'].'/'.$dirs[$i],
					'dir_name'=>$dirs[$i]
					);
			}
			else{
				$paths[]=array(
					'dir_path'=>$dirs[$i],
					'dir_name'=>$dirs[$i]
					);;
			}
		}
		
		return $paths;
	
	}
	
	public function rename($new_path){
		return rename($this->path, $new_path);
	}
	
	public function removeDir($children_names){
		for($i=0;$i<count($children_names);$i++){	
			$this->removeDirRecursive($this->path.'/'.$children_names[$i]);
		}
		return true;
	}
	
	private function removeDirRecursive($dir_name){
		$handle = opendir($dir_name);
		while (false !== ($file = readdir($handle))){		
			if($file=='.' || $file=='..') continue;
			$path = $dir_name.'/'.$file;
			if(is_dir($path)){
				$this->removeDirRecursive($path);
			}else{
				unlink($path);
			}
		}
		rmdir($dir_name);	
	}
	
	public function removeFile($children_names){
		if(is_array($children_names)){
			for($i=0;$i<count($children_names);$i++){
				$file=new Ab_File($this->path.'/'.$children_names[$i]);
				
				$file_type_obj=$file->getPathType();
				$file_type_obj->remove();
			}
		}
		else{
			$file=new Ab_File($this->path.'/'.$children_names);
			$file_type_obj=$file->getPathType();
			$file_type_obj->remove();
		}
		return true;
	}
	
	public function addChildDir($dir_name){
		return mkdir($this->path.'/'.$dir_name);
	}
	
	public function getType(){
		return 'directory';
	}
}
?>
